import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Manager {
	String name;
	String pwd;
	private DatabaseControl sql;
	public Manager(DatabaseControl sqlCon) {
		this.sql=sqlCon;
	}
	
	public void addNewBook() {
		Scanner sc = new Scanner(System.in);
		
		String book_no;
		String book_name;
		String classify;
		String author;
		int store_count;
		String summary;
		String community;
		String area;
		String publish_time;
		System.out.println("输入图书编号");
		book_no = sc.nextLine();
		
		System.out.println("输入书名");
		book_name = sc.nextLine();
		
		System.out.println("输入图书分类，人文/艺术/理工/体育/其他");
		classify = sc.nextLine();
		
		System.out.println("输入作者名");
		author = sc.nextLine();

		System.out.println("输入简介");
		summary = sc.nextLine();
		
		System.out.println("输入图书所在地");
		area = sc.nextLine();
		
		System.out.println("输入出版时间,日期格式:xxxx年x月x日");
		publish_time = sc.nextLine();

		System.out.println("输入出版社名");
		community = sc.nextLine();
		
		System.out.println("输入库存数");
		store_count = sc.nextInt();
		
		Connection conn=sql.getConn();
		if(sql.checkBookNoIsExist(book_no)) {
			System.out.println("该书已存在");
		}
		else {
			sql.addNewBook(book_no,book_name,classify,author,store_count,summary,area,publish_time,community);
//			String sql = "insert into book values (?,?,?,?,?,?,?,?,?)";
//			try {
//				PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(sql);
//	
//				stmt.setString(1, book_no);
//				stmt.setString(2, book_name);
//				stmt.setString(3, classify);
//				stmt.setString(4, author);
//				stmt.setInt(5, store_count);
//				stmt.setString(6, summary);
//				stmt.setString(7, area);
//				stmt.setString(8, publish_time);
//				stmt.setString(9, community);
//				stmt.executeUpdate();
//				System.out.println("添加新书成功！！");
//				System.out.println();
//			}catch (SQLException e) {
//				// TODO 自动生成的 catch 块
//				e.printStackTrace();
//			} 
		}
	}
	
	public void setBookCount(){
		Scanner sc = new Scanner(System.in);
		
		System.out.println("输入图书编号");
		String book_no=sc.nextLine();
		System.out.println("设置库存");
		String count=sc.nextLine();
		if(Integer.parseInt(count)<0||!this.isNumeric(count)) {
			System.out.println("库存数输入非法");
		}else{
			Connection conn=sql.getConn();
			try {
				Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
	    		ResultSet rset = stmt.executeQuery("select book_name from book where book_no="+book_no); 
	    		rset.next();
	    		String t=rset.getString("book_name");
	
				stmt.executeUpdate("update book set store_count="+count+" where book_no="+book_no);
				System.out.println("修改库存完成！");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("无此书");
				System.out.println();
			}
		}

	}

	public void deleteBookByCode() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入书籍编号:");
		String no=sc.nextLine();
		Connection conn=sql.getConn();
		try {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			stmt.executeUpdate("delete from book where book_no="+no);
			System.out.println("移除书籍完成！");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("无此书");
			System.out.println();
		}
	}
	public static boolean isNumeric(String str){
		   for (int i = str.length();--i>=0;){  
		       if (!Character.isDigit(str.charAt(i))){
		           return false;
		       }
		   }
		   return true;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

}
